/**
 * @Author:刘忻忻
 * @Date:20200616
 * @Describle:这是登录页
 */
import React from "react";
import { Modal,message, Button, Space,Input} from 'antd';
import axios from "axios"
import {isCookie,setCookie,getCookiename} from "../../utils/GetCookie";
import VerificationCode from "./Component_Login/VerificationCode";

import {URL} from '../../index'

export default class Login extends React.Component{
    constructor(props) {
        super(props);
        this.state={
            accountCode:"",
            password:"",
            code:"",
            inp_code:"",
            hasError:false,
            http:URL
        }
        this.get_login_state=this.get_login_state.bind(this)
        this.get_accountCode=this.get_accountCode.bind(this)
        this.get_passw=this.get_passw.bind(this)
        this.get_tx=this.get_tx.bind(this)
        this.get_inpcode=this.get_inpcode.bind(this);
    }

    get_accountCode(event){
        this.setState({
            accountCode:event.target.value
        });
    }
    get_passw(event){
        this.setState({
            password:event.target.value
        })
    }
    get_inpcode(event){
        this.setState({
            inp_code:event.target.value
        })
    }
    get_tx(txt){
        this.setState({
            code: txt
        })
    }
    get_login_state(){
        if(this.state.code.toLowerCase()!==this.state.inp_code){
            Modal.warning({
                title: 'Warning',
                content: '请正确输入验证码',
            });
        }else if(this.state.password===""){
            Modal.warning({
                title: 'Warning',
                content: '请输入密码',
            });
        }else{
            axios({
                method:"GET",
                url:this.state.http+"/management/counselor/login",
                params:{
                    accountCode:this.state.accountCode,
                    password:this.state.password
                }
            }).then(res=>{
                if (res.data.code===200) {
                    setCookie(res.data.name,res.data.id)
                    this.props.history.push('/menuLayout')
                }
                else if(res.data.code === 500){
                    this.props.history.push('/serverError')
                }
                else{
                    Modal.error({
                        title: '登录失败',
                        content: '账户或密码错误，请重新输入',
                    });
                }
            }).catch(function (error) {//异常处理
                Modal.error({
                    title: '登录失败',
                    content: '网络异常，请检查网络',
                })
            })
        }
    }

    render(){
        return (
            <div className="login_body">
                <div className="login_img">
                    <img src={require('../../access/images/login.jpg')}/>
                </div>
                <div className="login_container">
                    <table>
                        <tr><td>
                            <div className="login_title">
                                <h1>启明投顾平台</h1>
                            </div>
                        </td></tr>
                        <tr><td>
                            <div className="login_inp" >
                                <h3>管理员登录</h3>
                                <div ><Input className="login_input_ac" type="text" required={true} value={this.state.accountCode} onChange={this.get_accountCode} placeholder="请输入账号"></Input></div>
                                <br/>
                                <div ><Input className="login_input_pw" type="password" required={true} value={this.state.password} onChange={this.get_passw} placeholder="请输入密码"></Input></div>
                                <br/>
                                <div className="login_input_vc">
                                    <Input className="inp_but_tx"  type="text" required={true} value={this.state.inp_code} onChange={this.get_inpcode} placeholder="请输入验证码" />
                                    <div className="login_tx">
                                        <VerificationCode className="tx_pic" get_tx={this.get_tx}/>
                                    </div>
                                </div>
                                <button onClick={this.get_login_state } className="login_but" >登录</button>
                            </div>
                        </td></tr>
                    </table>
                </div>
            </div>
        )
    }
}
